import angular from 'angular'
import util from '@yunke/core/util'
import { map } from 'lodash'
import { removeHmS } from "../../../../contract/pages/util"

const api_prefix = window.G_CONFIG['api_prefix']

function ajax(title, url, get_data, callback, ajax_type, async1 = true, failedCallback) {
    return new Promise((resolve, reject) => {
        util.myAjax({
            url: api_prefix + url,
            async: async1,
            type: ajax_type || "GET",
            context: this,
            data: get_data,
            success(json) {
                if (+json.retCode === 0) {
                    let newVar = json.data;
                    if (typeof json.data === 'undefined') {
                        newVar = [];
                    }
                    if (callback) {
                        callback(newVar);
                    }
                    resolve(newVar);
                } else {
                    if (failedCallback) {
                        failedCallback();
                    }
                    const message = (ajax_type === 'GET' ? "获取" : '') + title + "失败：" + json.errMsg;
                    util.alert(message);
                    reject(new Error(message));
                }
            },
            error(err) {
                if (failedCallback) {
                    failedCallback();
                }
                reject(err);
            }
        });
    })
}

angular.module('ContractCommonService', []).service('contractCommonService', [() => {
    return {
        getCurTime() {
            /**
             * 获取2015-01-01 统一格式时间
             */
            const now = new Date();
            const now_y = now.getFullYear();
            const now_m = (now.getMonth() < 10) ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;
            const now_d = (now.getDate() < 10) ? '0' + now.getDate() : now.getDate();
            return now_y + '-' + now_m + '-' + now_d;
        },
        getContract(contractId, callback) {
            ajax('合同信息', "/contract/get-contract-info", { contractId }, callback, 'GET', false);
        },
        getContractNatures(callback) {
            ajax('合同性质', "/contract/get-contract-natures", {}, callback, 'GET', false);
        },
        getContractSignDelay(contractId, callback) {
            ajax('交付信息', '/contract/get-sign-dely', { contractId }, callback, 'GET');
        },
        getContractCategories(callback) {
            ajax('合同类别', "/contract/get-contract-categories", {}, callback, 'GET', false);
        },
        getContractBigClass(callback) {
            ajax('合同大类', "/contract/get-contract-big-class", {}, callback, 'GET', false);
        },
        getContractSmallClass(bigClassId, callback) {
            ajax('合同小类', "/contract/get-contract-small-class", { bigClassId }, callback);
        },
        getContractAreas(callback) {
            ajax('合同区域', "/contract/get-contract-area-list", {}, callback);
        },
        getCompanies(callback) {
            ajax('公司', "/erp-interface/get-companies", {}, callback);
        },
        getProducts(callback) {
            ajax('产品', "/contract/get-products", {}, callback);
        },
        getOtherProducts(callback) {
            ajax('其他产品', "/contract/get-products&other=1", {}, callback);
        },
        getBalanceProducts(contractId, callback) {
            ajax('产品', "/contract/get-contract-application-list", { contractId }, callback);
        },
        getBussAccountTypes(callback) {
            ajax('核算收入类别', "/contract/get-buss-account-types", {}, callback, 'GET', false);
        },
        getContractRevenue(contractId, callback) {
            return ajax('收入信息', "/contract/get-contract-revenue", { contractId }, callback, 'POST');
        },
        delRevenueByRevenueId(revenueId, callback) {
            return ajax('删除收入', "/revenue/del-revenue-by-revenue-id", { revenue_id: revenueId }, callback, 'POST');
        },
        getRevenueRecognitionList(revenueId, callback) {
            return ajax('结转信息', "revenue/get-revenue-recognition-list-by-revenue-id", { revenue_id: revenueId }, callback, 'GET');
        },
        updateRecognitionByRevenueId(recognitionId, recognitionAmount, callback) {
            return ajax('更新结转收入信息', "revenue/update-recognition-by-revenue-id", {
                recognition_id: recognitionId,
                recognition_amount: recognitionAmount
            }, callback, 'POST');
        },
        getContractOrganizationInfo(contractId, callback) {
            ajax('开通信息', "/contract/get-contract-organization-info", { contractId }, callback, 'POST');
        },
        getContractBalanceInfo(contractId, callback) {
            ajax('结算返款信息', "/contract/get-contract-balance-info", { contractId }, callback, 'POST');
        },
        getContractUnifiedInfo(contractId, callback) {
            ajax('合同应收信息', "/receivable/get-contract-unified-info", { contractId }, (d) => {
                d.receivables = map(d.receivables, (i) => {
                    i.department_name = i.department_name === false ? "" : i.department_name;
                    i.receivable_date = removeHmS(i.receivable_date)
                    return i;
                })
                return callback(d);
            }, 'GET');
        },
        getContractUnifiedInfoWithoutAuth(contractId, callback) {
            ajax('合同应收信息', "/receivable/get-contract-unified-info-without-auth", { contractId }, callback, 'GET');
        },
        getRelationIncome(contractId, callback) {
            ajax('关联收入项', "/contract/get-relation-income", { contractId }, callback, 'GET');
        },
        genContractFlowCode(areaCode, customerId, contractCode, callback) {
            ajax('合同编号', "/contract/get-contract-flow-code", { areaCode, customerId, contractCode }, callback, 'GET');
        },
        getContractReceivable(contractId, callback) {
            ajax('应收款信息', "/receivable/get-contract-receivable", { contractId }, callback, 'GET');
        },
        getReceivableFundName(callback) {
            ajax('应收款名称信息', "/contract/get-receivable-fund-name", {}, callback, 'GET', false);
        },
        getDisabledInvoice(payment_id, callback) {
            ajax('编辑回款默认信息', "/receivable/get-contract-payment", { payment_id }, callback, 'GET', false);
        },
        getInvoiceReceivableInfoByContract(contractId, callback) {
            ajax('发票与应收款对应关系', "/receivable/get-invoice-receivable-info-by-contract", { contractId }, callback, 'GET', false);
        },
        getCanUseInvoiceInfo(contractId, keyword, callback) {
            ajax('未关联的发票', "/receivable/get-can-use-invoice-info", { contractId, keyword }, callback, 'GET', true);
        },
        getContractPaymentHaveAmountByContract(contractId, callback) {
            ajax('未完全开完发票的回款记录', "/receivable/get-contract-payment-have-amount-by-contract", { contractId }, callback, 'GET', true);
        },
        actionGetContractAccountCompanyList(propertyId, callback) {
            ajax('帐套公司列表', "/contract/get-contract-account-company-list", { propertyId }, callback, 'GET', false);
        },
        actionGetContractCompanyList(propertyId, callback) {
            ajax('签署公司列表', "/contract/get-contract-company-list", { propertyId }, callback, 'GET', false);
        },
        getFundSmallClass(propertyId, callback) {
            ajax('款项小类', "/contract/get-fund-small-class", { propertyId }, callback, 'GET', false);
        },
        getInvoiceBusinessUnit(callback) {
            ajax('开票单位', "/invoice/get-invoice-business-unit", { keyword: '' }, callback, 'GET', false);
        },
        getAccountHkList(accountName, callback, failedBack) {
            ajax('财务系统的回款信息', "/erp-interface/get-account-hk-list", { paymentAccountName: accountName }, callback, 'GET', true, failedBack);
        },
        getAgencyCompanyList(company_name, callback) {
            ajax('服务列表', '/contract/get-agency-company-list', { company_name }, callback, 'GET', false);
        }
    }
}]);
